/*
 *
 *  Copyright (C) 2009-2011 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@libresoft.es>
 *
 */

package com.sitem.appsjam.Types;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.sitem.appsjam.Utils.BitmapUtils;


public class Punt {

	private Integer mId;
	private Double mLatitude;
	private Double mLongitude;
	private Double mAltitude;
	private String urlInfo;
	private String mName = null;
	private int mTipus = 0;
	private String mDescription = null;
	
	private String mPhotoUrl = null; //Remot
	private String mPhotoPath = null; //Local
	private boolean visibility = false;
	
	byte[]  mByteBitMapImageThumb, mByteBitMapImage;

	private Layer mFatherLayer = null;

	private boolean incidencia;
	
	
	/**
     * Constructor used to create this object. 
     * @param id Identifier of social network (can be 0)
     * @param latitude The latitude of the node position 
     * @param longitude The longitude of the node position
     * @param altitude The Altitude of the node position
     * @param radius The radius/error of the node position
     * //@param since The time when the node was created
     */
	
	public Punt (Integer id, Double latitude, Double longitude, Double altitude) {
		mId = id;
		
		mLatitude = latitude;
		mLongitude = longitude;
		mAltitude = altitude;
		
		mTipus = 0;
		mName = "";
		mDescription = "";
		mPhotoUrl = "";
		mByteBitMapImageThumb = null;
		mByteBitMapImage = null;
	}
	
	public Punt (Integer id, Double latitude, Double longitude, Double altitude, 
			      String name, String description, int tipus) 
	{
		mId = id;
		
		mLatitude = latitude;
		mLongitude = longitude;
		mAltitude = altitude;
		
		mTipus = tipus;
		mName = name;
		mDescription = description;
		mByteBitMapImageThumb = null;
		mByteBitMapImage = null;
	}


	public GeoPoint toGeoPoint() {
		return new GeoPoint((int)(mLatitude*1E6),(int)(mLongitude*1E6));
	}

	public Location toLocation()
	{
		Location myLocation = new Location("gps");
		myLocation.setLatitude(mLatitude);
		myLocation.setLongitude(mLongitude);
		myLocation.setAltitude(mAltitude);
		return myLocation;
	}
	
	
	public void setVisibility(boolean checked) {
		visibility = checked;
	}

	public boolean getVisibility() {
		return visibility;
	}
	
	public int getTipus() {
		return mTipus;
	}
	
	public void setTipus(int mTipus) {
		this.mTipus = mTipus ;
	}
	
	public String getName() {
		return mName;
	}

	public void setName(String name) {
		mName = name;
	}

	public String getDescription() {
		return mDescription;
	}

	public void setDescription(String description) {
		mDescription = description;
	}

	public String getPhotoUrl() {
		return mPhotoUrl;
	}

	public void setPhotoUrl(String url) {
		mPhotoUrl = url;
	}
	
	public String getPhotoPath()
	{
		return mPhotoPath;
	}
	
	public void setPhotoPath (String path)
	{
		mPhotoPath = path;
	}
	
	public boolean isBitmapPhotoThumb()
	{
		return mByteBitMapImageThumb != null;
	}

	public Bitmap getBitmapPhotoThumb(Context ct)
	{
		//Log.i("Node Punt","Demanant imatge thumbnail");
		if (mByteBitMapImageThumb == null)
		{
			try{
				Bitmap bitmapImage = null;
				
				if(mByteBitMapImage != null) {
					bitmapImage = BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImage) );
				} else if (mPhotoPath != null) {
					bitmapImage = BitmapUtils.loadBitMapFromFile(mPhotoPath);
				} else {
					bitmapImage = BitmapUtils.loadBitmap(mPhotoUrl);
					if (bitmapImage == null) {
						Log.e("Punt", "Punt sense foto! ("+mPhotoUrl+")");
						//TODO: Tractar el cas i mostrar nomes el text, o una imatge especial.
						mByteBitMapImageThumb = null;
						return null;
					}
				}

				if((bitmapImage.getHeight()*bitmapImage.getWidth()) > 57600){ // 240x240
					if(bitmapImage.getWidth() > bitmapImage.getHeight()) {
						bitmapImage = Bitmap.createScaledBitmap(bitmapImage, 240, (int)(((double)bitmapImage.getHeight()/(double)bitmapImage.getWidth())*240), true);
					} else {
						bitmapImage = Bitmap.createScaledBitmap(bitmapImage, (int)(((double)bitmapImage.getWidth()/(double)bitmapImage.getHeight())*240), 240, true);
					}
				}

				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos)) {
					Log.e("getBitmapImageThumb","Error: Don't compress de image");
					return null;
				}

				mByteBitMapImageThumb = baos.toByteArray();

			}catch(Exception e){
				Log.e("Punt", "", e);
				mByteBitMapImageThumb = null;
				return null;
			}

		}
		
		if (mByteBitMapImageThumb == null) return null;
		
		return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImageThumb) );
	}
	
	public void clearBitmapPhotoThumb(){
		mByteBitMapImageThumb = null;
	}
	
	public void clearBitmapPhoto(){
		mByteBitMapImage = null;
	}
	
	public boolean isBitmapPhoto()
	{
		return mByteBitMapImage != null;
	}

	public Bitmap getBitmapPhoto(Context ct)
	{
		//Log.i("Node Punt","Demanant imatge gran");
		if (mByteBitMapImage == null)
		{
			try{
				Bitmap bitmapImage = null;

				if (mPhotoUrl != null) {
					bitmapImage = BitmapUtils.loadBitmap(mPhotoUrl);
				} else if (mPhotoPath != null) {
					bitmapImage = BitmapUtils.loadBitMapFromFile(mPhotoPath);
				}

				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				if (!bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos)) {
					Log.e("getBitmapImage","Error: Don't compress de image");
					return null;
				}

				mByteBitMapImage = baos.toByteArray();

			}catch(Exception e){
				Log.e("Punt", "", e);
				mByteBitMapImage = null;
				return null;
			}

		}
		
		if (mByteBitMapImage == null)
			return null;
		
		return BitmapFactory.decodeStream( new ByteArrayInputStream( mByteBitMapImage) );
	}
	

	public int describeContents() {
		return 0;
	}

	public void setIncidencia(boolean b) {
		incidencia = b;
	}
	
	public boolean isIncidencia() {
		return incidencia;
	}


	public String getUrlInfo() {
		return urlInfo;
	}

	public void setUrlInfo(String urlInfo) {
		this.urlInfo = urlInfo;
	}

	
	public void setLayer (Layer layer)
	{
		mFatherLayer = layer;
	}
	
	public Layer getLayer ()
	{
		return mFatherLayer;
	}
	
	/**
     * Return the Identifier associated to social network
     */
	
	public Integer getId() {
		return mId;
	}
	
	/**
     * Return the latitude of the node
     */
	public Double getLatitude() {
		return mLatitude;
	}
	
	/**
     * Return the longitude of the node
     */
	public Double getLongitude() {
		return mLongitude;
	}
	
	/**
     * Return the altitude of the node
     */
	public Double getAltitude(){
		return mAltitude;
	}
	
	/**
     * Set the latitude of the node
     * 
     * @param latitude The latitude of the position node
     */
	public void setLatitude(Double latitude){
		mLatitude = latitude;
	}
	
	/**
     * Set the longitude of the node
     * 
     * @param longitude The latitude of the position node
     */
	public void setLongitude(Double longitude){
		mLongitude = longitude;
	}
	
	
	public void setAltitude(double altitude){
		mAltitude = altitude;
	}
	
	public void setId(Integer id){
		mId = id;
	}

	
}
